package com.admin.service.ipl;

import com.admin.mapper.AdUserMapper;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;


import com.usian.utils.common.AppJwtUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


@Service
public class AdUserService  extends ServiceImpl<AdUserMapper, AdUser> implements com.admin.service.AdUserService {


    @Override
    public ResponseResult login(AdUserDto dto) {
//        判断账号密码是否为空
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_REQUIRE,"账号或密码不能为空");
        }
//        查询
        Wrapper wrapper=new QueryWrapper();
        ((QueryWrapper)wrapper).eq("name",dto.getName());
        AdUser one = getOne(wrapper);
//        根据名称查询 看是否有账号
        if (one!=null){
//            再判断用户密码 md5格式转换
            String m= DigestUtils.md5DigestAsHex((dto.getPassword()+one.getSalt()).getBytes());
            if (m.equals(one.getPassword())){
                Map<String ,Object> c=new HashMap<>();
//                存到map中 并返回页面
                c.put("password","");
                c.put("salt","");
                c.put("user",one);
                c.put("token", AppJwtUtil.getToken(one.getId().longValue()));
                return ResponseResult.okResult(c);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
            }


        }else {
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"账号不存在");
        }



    }
}
